package org.copper.ui
{
	public class CVList extends BaseCList
	{
		public static const TO_CENTER:uint = 0;
		public static const TO_RIGHT:uint = 1;
		public static const TO_LEFT:uint = 2;
		
		
		private var m_valign:uint = TO_CENTER;
		private var m_dataHeight:Number = 0;
		
		private var m_avarageItmScrollSize:Number = 0;
		
		public function CVList()
		{
			super();
		}
		
		override protected function onDataProviderUpdated(dp:Object, pos:Number, container:CSprite):void
		{
			const dataLen:uint = dp.length;
			this.removeAllFromContainer();
			const h:Number = height;
			if (dataLen > 0)
			{
				var calculateX:Function = calculateXToCenter;
				switch(m_valign)
				{
					case TO_RIGHT:
						calculateX = calculateXToRight;
						break;
					case TO_LEFT:
						calculateX = calculateXToLeft;
						break;
				}
				
				var prevY:Number = 0;
				for(var i:uint = 0; i < dataLen; i++)
				{
					const renderer:CSprite = getRenderer();
					renderer.data = dp[i];
					renderer.y = prevY;
					calculateX(renderer);
					prevY += renderer.height;
					container.addChild(renderer);
				}
				m_dataHeight = prevY;
				
				const maxScrollLen:Number = 1 - (h / m_dataHeight);
				maxScroll = maxScrollLen;
				m_avarageItmScrollSize = maxScrollLen/dataLen;
			}
			else
			{
				m_avarageItmScrollSize = 0;
			}
			
			
			onScrollPositionUpdate(pos, pos);
		}
		
		override protected function onScrollPositionUpdate(newPosition:Number, oldPosition:Number):void
		{
			this.scrollY = m_dataHeight * newPosition;
			this.render();
		}
		
		override protected function onTransform(offsetX:Number, offsetY:Number):void
		{
			if (offsetY != 0 && m_dataHeight != 0)
			{
				scrollPosition += offsetY / m_dataHeight;
			}
		}
		
		
		
		protected function calculateXToLeft(renderer:CSprite):void
		{
			renderer.x = 0;
		}
		
		protected function calculateXToCenter(renderer:CSprite):void
		{
			renderer.x = (this.width - renderer.width) * 0.5;
		}
		
		protected function calculateXToRight(renderer:CSprite):void
		{
			renderer.x = this.width - renderer.width;
		}
		
		public function get valign():uint
		{
			return m_valign;
		}
		
		public function set valign(value:uint):void
		{
			if (value != m_valign)
			{
				switch(value)
				{
					case TO_CENTER:
						m_valign = TO_CENTER;
						break;
					case TO_RIGHT:
						m_valign = TO_RIGHT;
						break;
					case TO_LEFT:
						m_valign = TO_LEFT;
						break;
					default:
						m_valign = TO_CENTER;
						break;
				}
				forceUpdate();
			}
		}
	}
}